﻿using System;
using System.Linq;

using VirtualTournaments.DataAccess.SqlServer.DataProxies;

namespace VirtualTournaments.DataAccess.SqlServer.DataMappers
{
	internal sealed class ThreeWaysBetTypesDataMapper : AbstractDataMapper<SqlServer.BetType, Business.Bets.ThreeWaysBetType, DataProxies.ThreeWaysBetTypeDataProxy>
	{
		#region Singleton implementation

		private static ThreeWaysBetTypesDataMapper _instance = null;
		private static object _instanceLocker = new object();

		private ThreeWaysBetTypesDataMapper() { }

		public static ThreeWaysBetTypesDataMapper Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_instanceLocker)
					{
						if (_instance == null)
						{
							_instance = new ThreeWaysBetTypesDataMapper();
						}
					}
				}
				return _instance;
			}
		}

		#endregion

		internal override SqlServer.VirtualTournamentsDataContext GetDataContext()
		{
			return VirtualTournamentsDataContext.Instance;
		}

		internal override System.Data.Linq.Table<SqlServer.BetType> GetDataTable()
		{
			return GetDataContext().BetTypes;
		}

		internal override Business.Bets.ThreeWaysBetType DataObject2BusinessObject(SqlServer.BetType dataObject)
		{
			Business.Tournaments.Match match = MatchesDataMapper.Instance.GetOneAsBusinessObject(_match => _match.MatchId == dataObject.MatchId);
			return new ThreeWaysBetTypeDataProxy(dataObject, match);
		}

		internal override void BusinessObject2DataObject(Business.Bets.ThreeWaysBetType bizBetType, SqlServer.BetType dataBetType)
		{
			dataBetType.Kind = (int)BetTypeKind.ThreeWaysBetType;

			if (bizBetType.Match != null)
			{
				//dataBetType.CompetitionId = MatchesDataMapper.Instance.Save(bizBetType.Match);
			}

			dataBetType.BetTargets.Clear();
			if (bizBetType.BetTargets != null && bizBetType.BetTargets.Count > 0)
			{
				foreach (Business.Bets.ThreeWaysBetTarget bizBetTarget in bizBetType.BetTargets.Cast<Business.Bets.ThreeWaysBetTarget>())
				{
					SqlServer.BetTarget dataBetTarget = new SqlServer.BetTarget();
					dataBetTarget.Kind = (int)BetTargetKind.ThreeWaysBetTarget;
					dataBetTarget.Odds = Convert.ToDouble(bizBetTarget.Odds);
					dataBetTarget.ThreeWaysBetTarget = new SqlServer.ThreeWaysBetTarget();
					dataBetTarget.ThreeWaysBetTarget.ExpectedResult = (int)bizBetTarget.ExpectedResult;
					dataBetTarget.ThreeWaysBetTarget.MatchId = dataBetType.MatchId.Value;
					dataBetType.BetTargets.Add(dataBetTarget);

					dataBetTarget.BetTargetBets.Clear();
					if (bizBetTarget.Bets != null && bizBetTarget.Bets.Count > 0)
					{
						foreach (Business.Bets.Bet bizBet in bizBetTarget.Bets)
						{
							SqlServer.BetTargetBet dataBetTargetBet = new SqlServer.BetTargetBet();
							dataBetTargetBet.Bet = BetsDataMapper.Instance.Save(bizBet);
							dataBetTarget.BetTargetBets.Add(dataBetTargetBet);
						}
					}
				}
			}
		}

		internal override void DataProxy2DataObject(ThreeWaysBetTypeDataProxy dataProxy, BetType dataObject)
		{
			throw new NotImplementedException();
		}
	}
}
